/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import beans.Judge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xml.judge.JudgeReader;
import xml.judge.JudgeWriter;

public class DaoJudge implements IDAO<Judge> {
    
    JudgeWriter writer;
    JudgeReader reader;
    static Logger logger;

    public DaoJudge() {
        writer = new JudgeWriter();
        reader = new JudgeReader();
        logger = LoggerFactory.getLogger(DaoJudge.class);
    }

    private Long getId() {
        Properties props = new Properties();
        File propsFile = new File("src/xml/judge/judgeSequence.properties");
        boolean needLoadPropsFile = true;

        if (!propsFile.exists()) {
            try {
                propsFile.createNewFile();
                props.load(new FileInputStream(propsFile));
                props.setProperty("lastId", "0");
                props.store(new FileOutputStream(propsFile), "Save the last beer id");
                needLoadPropsFile = false;
            } catch (IOException ex) {
                logger.error("Error to create the properties file", ex);
                return null;
            }
        }

        FileInputStream file;
        try {
            if (needLoadPropsFile) {
                file = new FileInputStream(propsFile);
                props.load(file);
            }
            Long lastId = new Long(props.getProperty("lastId"));
            Long idToSave = lastId + 1;
            props.setProperty("lastId", (idToSave).toString());
            OutputStream fosProps = new FileOutputStream(propsFile);
            props.store(fosProps, "Save the last beer id");
            return lastId;
        } catch (FileNotFoundException ex) {
            logger.error("Sequence file not found", ex);
            return null;
        } catch (IOException ex) {
            logger.error("Error while load or store the properties file", ex);
            return null;
        }
    }
    
    @Override
    public boolean save(Judge entity) {
        Judge j = reader.getJudgeByName(entity.getName());
        if(j != null){
            logger.warn("Judge name already exists");
            return false;
        }
        j = reader.getJudgeByBJCPId(entity.getBjcpId());
        if(j != null){
            logger.warn("Judge BJCP id already exists");
            return false;
        }
        if (entity != null && entity.getId() == null) {
            Long id = getId();
            if (id == null) {
                return false;
            } else {
                entity.setId(id);
            }
        }

        return writer.print(entity);
    }

    @Override
    public boolean update(Judge entity) {
        Judge oldJudge = reader.getJudgeById(entity.getId());
        if (writer.remove(oldJudge)) {
            if (writer.print(oldJudge)) {
                logger.info("Success to update beer");
                return true;
            } else {
                if (writer.print(oldJudge)) {
                    logger.error("Error to update beer: " + oldJudge);
                } else {
                    logger.error("Error to update beer: " + oldJudge + " the beer was deleted and can't recovered");
                }
            }
        } else {
            logger.error("Error to delete old beer");
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        Judge oldJudge = reader.getJudgeById(id);
        return writer.remove(oldJudge);
    }

    @Override
    public Judge getById(Long id) {
        return reader.getJudgeById(id);
    }

    @Override
    public List<Judge> getAll() {
        return reader.getAllJudges();
    }

    public Judge login(String bjcpId, String password) {
        Judge judge = reader.getJudgeByBJCPId(bjcpId);
        if(judge != null && judge.getSenha().equals(password)){
            return judge;
        }
        return null;
    }

    public Judge getBjcpId(String bjcpId) {
        return reader.getJudgeByBJCPId(bjcpId);
    }

}
